﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;

namespace com.huigo.B2B.Web.center.invoice
{
    public partial class Invoice : BasePage
    {
        /// <summary>
        /// 订单编号
        /// </summary>
        private string invoiceOrderNo
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["orderNo"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["orderNo"]);
            }
        }
        /// <summary>
        /// 订单开始时间
        /// </summary>
        private DateTime queryStart
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["Start"]) ? Convert.ToDateTime(Request.QueryString["Start"]) : DateTime.MinValue;
            }
        }
        /// <summary>
        /// 订单结束时间
        /// </summary>
        private DateTime queryEnd
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["End"]) ? Convert.ToDateTime(Request.QueryString["End"]) : DateTime.MinValue;
            }
        }
        /// <summary>
        /// 产品ID
        /// </summary>
        private long product
        {
            get
            {
                return com.loto.Common.Utils.IsNumeric(Request.QueryString["productId"]) ? Convert.ToInt64(com.loto.Common.Utils.SqlEscape(Request.QueryString["productId"])) : long.MinValue;
            }
        }
        /// <summary>
        /// 订单状态
        /// </summary>
        private int queryOrderType
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["orderType"]) ? int.MinValue : Convert.ToUInt16(com.loto.Common.Utils.SqlEscape(Request.QueryString["orderType"]));
            }
        }
        private int pageIndex
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["page"], 1);
            }
        }
        public string Notiy = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        private void BindData()
        {
            if (!string.IsNullOrEmpty(invoiceOrderNo))
            {
                orderNo.Text = invoiceOrderNo;
            }
            if (queryStart != DateTime.MinValue)
            {
                beginDate.Text = Convert.ToDateTime(queryStart).ToString("yyyy-MM-dd");
            }
            if (queryEnd != DateTime.MinValue)
            {
                endDate.Text = Convert.ToDateTime(queryEnd).ToString("yyyy-MM-dd");
            }
            List<RequestFuncParams> list = new List<RequestFuncParams>();
            list.Add(new RequestFuncParams("custFid", CustId.ToString()));
            list.Add(new RequestFuncParams("orderNo", invoiceOrderNo));
            list.Add(new RequestFuncParams("start", queryStart.ToString()));
            list.Add(new RequestFuncParams("end", queryEnd.ToString()));
            list.Add(new RequestFuncParams("type", queryOrderType.ToString()));
            list.Add(new RequestFuncParams("productId", product.ToString()));
            list.Add(new RequestFuncParams("pageSize", pageInfo.PageSize.ToString()));
            list.Add(new RequestFuncParams("pageIndex", pageIndex.ToString()));
            BaseService service = new BaseService();
            ParserXml parser = service.RequestEntryService(Dict.ERequestFunc.GET_INVOICE_ORDER_LIST, list);
            if (parser != null && parser.RowsCount > 0)
            {
                pageInfo.RecordCount = Convert.ToInt16(parser["RecordCount", 0]);
                pageInfo.CurrentPageIndex = pageIndex;
                pageInfo.PageURLFormat = GetPagerUrl();
                OrderListContent.DataSource = parser.ModelList;
                OrderListContent.DataBind();
            }
            else
            {
                Notiy = "<table width=\"100%\" class=\"table table-bordered table-striped responsive-utilities table-hover\"><thead><tr><th>订单号</th><th>保险产品</th><th>申领张数</th><th>实发张数</th><th>配送方式</th><th>订单金额</th><th>订单时间</th><th>订单状态</th></tr></thead><tbody><tr><td colspan=\"8\" style=\"text-align:center;\">没有相关信息</td></tr></tbody></table>";
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string url = "Invoice.aspx?";
            url += "page=1";
            if (orderNo.Text != string.Empty)
                url += "&orderNo=" + orderNo.Text;
            if (beginDate.Text != string.Empty)
                url += "&Start=" + beginDate.Text;
            if (endDate.Text != string.Empty)
                url += "&End=" + endDate.Text;
            if (insList.SelectedItem.Text != string.Empty)
                url += "&productId=" + insList.SelectedValue;
            if (statusList.SelectedValue != string.Empty)
                url += "&orderType=" + statusList.SelectedValue;
            Response.Redirect(url);
        }

        protected void pageInfo_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBind();
                DropSelected();
                BindData();
            }
        }
        private string GetPagerUrl()
        {
            string url = "Invoice.aspx?";
            url += "page={0}";
            if (invoiceOrderNo != string.Empty)
                url += "&orderNo=" + invoiceOrderNo;
            if (queryStart != DateTime.MinValue)
                url += "&Start=" + queryStart;
            if (queryEnd != DateTime.MinValue)
                url += "&End=" + queryEnd;
            if (product != long.MinValue)
                url += "&productId=" + product;
            if (queryOrderType != int.MinValue)
                url += "&orderType=" + queryOrderType;
            return url;
        }
        public string Shipping(string typeId,string orderId)
        {
            string content = string.Empty;
            if (typeId == "1")
            {
                content = "寄付<br /><div class=\"wrap_layout_track\"><a class=\"js_order_address\" href=\"javascript:void(0);\" order_id=\"" + orderId + "\">配送地址<b class=\"arrow_piont\"></b></a><div class=\"layout_popup\"><div class=\"layout_inner order_box\"><div class=\"layout_hd\"><h4>配送地址</h4></div><div class=\"layout_bd\"></div></div><div class=\"layout_arrow_top\"><span class=\"\">◆</span><i>◆</i></div></div></div>";
            }
            if (typeId == "2")
            {
                content = "到付<br /><div class=\"wrap_layout_track\"><a class=\"js_order_address\" href=\"javascript:void(0);\" order_id=\"" + orderId + "\">配送地址<b class=\"arrow_piont\"></b></a><div class=\"layout_popup\"><div class=\"layout_inner order_box\"><div class=\"layout_hd\"><h4>配送地址</h4></div><div class=\"layout_bd\"></div></div><div class=\"layout_arrow_top\"><span class=\"\">◆</span><i>◆</i></div></div></div>";
            }
            if (typeId == "3")
            {
                content = "自取";
            }
            return content;
        }
        public string orderStatus(string typeId,string orderId)
        {
            if (typeId == "4")
            {
                return "<br /><div class=\"wrap_layout_track\"><a class=\"js_order_fedex\" href=\"javascript:void(0);\" order_id=\"" + orderId + "\">物流信息<b class=\"arrow_piont\"></b></a><div class=\"layout_popup\"><div class=\"layout_inner order_box\"><div class=\"layout_hd\"><h4>物流信息</h4></div><div class=\"layout_bd\"></div></div><div class=\"layout_arrow_top\"><span class=\"\">◆</span><i>◆</i></div></div></div>";
            }
            else
            {
                return string.Empty;
            }
        }
        private void LoadBind()
        {
            List<DropList> statusDropList = Tools.DictToDropList(Dict.DictInvoiceStatus);
            if (statusDropList != null)
            {
                statusList.DataSource = statusDropList;
                statusList.DataValueField = "Id";
                statusList.DataTextField = "Value";
                statusList.DataBind();
            };
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            BaseService baseService = new BaseService();
            ParserXml parserXml = baseService.RequestEntryService(Dict.ERequestFunc.GET_PRODUCT_INFO_LIST, argList);
            List<DropList> cDropList = Tools.ParserToDropList(parserXml, "ProductId", "ProductName");
            if (cDropList != null)
            {
                insList.DataSource = cDropList;
                insList.DataValueField = "Id";
                insList.DataTextField = "Value";
                insList.DataBind();
            }
        }
        /// <summary>
        /// Dropdownlist选中
        /// </summary>
        private void DropSelected()
        {
            if (statusList.Items.Count > 0)
            {
                for (int i = 0; i < statusList.Items.Count; i++)
                {
                    if (statusList.Items[i].Value == queryOrderType.ToString())
                    {
                        statusList.SelectedIndex = i;
                    }
                }
            }
            if (insList.Items.Count > 0)
            {
                for (int i = 0; i < insList.Items.Count; i++)
                {
                    if (insList.Items[i].Value == product.ToString())
                    {
                        insList.SelectedIndex = i;
                    }
                }
            }          
        }
    }
}
